home *** CD-ROM | disk | FTP | other *** search
- Path: hubcap.clemson.edu!hubcap!mjs
- From: mjs@hubcap.clemson.edu (M. J. Saltzman)
- Newsgroups: comp.lang.c
- Subject: Re: help rand !
- Date: 19 Apr 96 15:03:59 GMT
- Organization: Clemson University
- Message-ID: <mjs.829926239@hubcap>
- References: <4l5d75$mes@usenetp1.news.prodigy.com> <4l5fj4$1ehs@serra.unipi.it> <4l5vjm$5lj@ccshst05.uoguelph.ca> <4l7bnt$r44@griffin.itc.gu.edu.au>
- NNTP-Posting-Host: hubcap.clemson.edu
- X-Newsreader: NN version 6.5.0 #1
-
- Student (Student) writes:
-
- |I assume you want something that does this:
- |10 random -> 9,3,5,1,7,8,2,4,10,6
- |You could consider creating an array n[n]
- ^^^^One of these things needs to be
- called something else.
- |where n represents the highest number
- |and then just randomly swaping the contents for every n.
- |ie: before: n[1] = 1
- | n[2] = 2
- | : :
- | n[10]=10
-
- Of course, in comp.lang.c, we index our arrays from 0 to n-1:
- ie: before: n[0] = 0
- n[2] = 2
- : :
- n[9] = 9
-
- |swap n[1] with n[rand]
- |swap n[2] with n[rand]
- |......................
- |swap n[10] with n[rand]
- |
- |after: n[1] = 9
- | n[2] = 3
- | : :
- | n[10]= 6
-
- |This ain't that great, but it will work predictably.
-
- It will work better if you make sure that the random index rand lies
- between the current index and the end of the array (inclusive). If
- you take any random index then the permutations are not all equally
- likely.
- --
- Matthew Saltzman
- Clemson University Math Sciences
- mjs@clemson.edu
-